package com.filldream.fastboot.core.manager;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.filldream.fastboot.base.entity.BaseParam;
import com.filldream.fastboot.base.mapper.BaseParamMapper;
import com.filldream.fastboot.base.request.common.baseParam.BaseParamListSTO;
import com.filldream.fastboot.common.entity.B;
import com.filldream.fastboot.common.entity.ErrorCode;
import com.filldream.fastboot.common.util.Assert;
import com.filldream.fastboot.core.cache.CacheMap;
import com.filldream.fastboot.core.cache.CacheObj;
import com.filldream.fastboot.core.cache.CacheUtil;
import org.apache.commons.lang3.ObjectUtils;
import org.springframework.stereotype.Component;
import javax.annotation.PostConstruct;
import javax.annotation.Resource;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author RickSun
 * @Description baseParamMapper
 * @date 2022/4/22 15:21
 * @return
 **/
@Component
public class BaseParamManager {

    @Resource
    private BaseParamMapper baseParamMapper;

    private static BaseParamManager self;

    @PostConstruct
    void init() {
        self = this;
        self.baseParamMapper = this.baseParamMapper;
    }

    public static List<BaseParam> getListByCode(BaseParamListSTO baseParamListSTO){
        String paramCode = baseParamListSTO.getParamCode();
        CacheObj<List<BaseParam>> cacheList = CacheUtil.getCacheList(CacheMap.BASE_PARAM_LIST, BaseParam.class, paramCode);
        String cacheKey = cacheList.getCacheKey();
        List<BaseParam> list = cacheList.getData();

        if( ObjectUtils.isEmpty(list) ){
            list = self.baseParamMapper.selectList(
                    new QueryWrapper<BaseParam>().eq("param_code", baseParamListSTO.getParamCode()));
            if(ObjectUtils.isNotEmpty(list)){
                CacheUtil.cacheObj(cacheKey,list);
            }
        }

        if( baseParamListSTO.getIsContainParent() == false){
           list = list.stream().filter(item -> !item.getParentId().equals(0L)).collect(Collectors.toList());
        }

        if( baseParamListSTO.getParamType() != null ){
            list = list.stream().filter(item -> item.getParamType().equals(baseParamListSTO.getParamType())).collect(Collectors.toList());
        }
        if(baseParamListSTO.getLinkId() != null){
            list = list.stream().filter(item -> item.getLinkId().equals(baseParamListSTO.getLinkId())).collect(Collectors.toList());
        }

        return list;
    }

    public static BaseParam getOneByCode(BaseParamListSTO baseParamListSTO){
        List<BaseParam> listByCode = getListByCode(baseParamListSTO);
        return ObjectUtils.isNotEmpty(listByCode) ? listByCode.get(0) : null;
    }

    public static BaseParam getById(Long id){
        CacheObj<BaseParam> cache = CacheUtil.getCache(CacheMap.BASE_PARAM_DETAIL, BaseParam.class, id);
        String cacheKey = cache.getCacheKey();
        BaseParam data = cache.getData();

        if( ObjectUtils.isEmpty(data) ){
            data = self.baseParamMapper.selectById(id);
            if(ObjectUtils.isNotEmpty(data)){
                CacheUtil.cacheObj(cacheKey,data);
            }
        }

        return data;
    }

    public static boolean del(List<Long> ids){
        Assert.isEmpty(ids,"请选择需要删除的参数");

        if( ids.size() == 1 ){
            Long id = ids.get(0);
            BaseParam oneParam = getById(id);
            Assert.isEmpty(oneParam,"有参数不存在，请刷新后重试");
            Long hasChild = self.baseParamMapper.selectCount(new QueryWrapper<BaseParam>().eq("parent_id",id));
            Assert.isBoolean( hasChild > 0, ErrorCode.BASE_ERR.reMsg("请先删除子级参数"));

            //删除和删除缓存
            self.baseParamMapper.deleteById(id);
            delCacheDetailById(id);
            delCacheListByCode(oneParam.getParamCode());

            return true;
        }else{
            for (Long id : ids) {
                del(Arrays.asList(id));
            }
        }
        return true;
    }

    public static B delCacheListByCode(String code){
        String keyName = CacheMap.BASE_PARAM_LIST.getKeyName(code);
        CacheUtil.delKey(keyName);
        return B.success();
    }

    public static B delCacheDetailById(Long id){
        String keyName = CacheMap.BASE_PARAM_DETAIL.getKeyName(id);
        CacheUtil.delKey(keyName);
        return B.success();
    }

    public static B delCacheDetailByIds(List<Long> ids){
        for (Long id : ids) {
            delCacheDetailById(id);
        }
        return B.success();
    }

}
